﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using FineUI;

public partial class Manager_ArticlesAdd : System.Web.UI.PageEx
{
    private string strPageCode = "B0022";
    private string strLoginResult = string.Empty;
    private int iSelectId = 0;
    private int iSelectClassId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["ArticleId"]), out iSelectId);
        int.TryParse(Convert.ToString(Request.QueryString["Class"]), out iSelectClassId);
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            btnReset.OnClientClick = sfObject.GetResetReference();
            DataBinds();
        }
    }
    private void DataBinds()
    {

        if (iSelectId > 0)//如果产品ID>0时绑定产品属性
        {
            Model.DbArticles dbArticle = DalEx.DbArticlesEx.Select(iSelectId);
            if (dbArticle != null)
            {
                hfArticleId.Text = dbArticle.AId.ToString();
                txtTitle.Text = dbArticle.ATitle;
                txtKeywords.Text = dbArticle.AKeywords;
                txtDescription.Text = dbArticle.ADescription;
                dpAddTime.Text = dbArticle.AAddTime.ToString("yyyy-MM-dd");
                //UEditor1.Value = HttpUtility.HtmlDecode(dbArticle.ANote);
                txtHits.Text = Convert.ToString(dbArticle.AHits);
                txtOrder.Text = Convert.ToString(dbArticle.AOrder);
                dpEndDate.Text = dbArticle.CEndDate.ToString("yyyy-MM-dd");

                txtaAllClassIds.Text = dbArticle.AAllClassIds;
                //txtaClassId.Text = dbArticle.AClassId.ToString();
                
                //List<string> lst = DalEx.DbArticlesEx.GetAllClassList(dbArticle.AClassIds);
                //foreach (FineUI.CheckItem lItem in cblClass.Items)
                //{
                //    if (lst.Contains(lItem.Value))
                //        lItem.Selected = true;
                //}

                PageContext.RegisterStartupScript(String.Format("updateUEditor1({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.ANote))));
                PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.AShortInstr))));
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtTitle.Text.Trim()))
        {
            FineUI.Alert.Show("请输入活动名称！");
            return;
        }
        if (string.IsNullOrEmpty(txtaAllClassIds.Text.Trim()))
        {
            FineUI.Alert.Show("请输入活动组织！");
            return;
        }
        if (string.IsNullOrEmpty(dpAddTime.Text.Trim()))
        {
            FineUI.Alert.Show("请选择活动时间！");
            return;
        }
        if (string.IsNullOrEmpty(txtHits.Text.Trim()))
        {
            FineUI.Alert.Show("请输入名额限制！");
            return;
        }
        
        Int16 iHits = 0, iOrder = 0, iStatus = 0, iIsPhotoNews = 0;
        int iSelectId = 0, iSelectClassId=0;

        DateTime dtEndDate = SqlHelper.MinDate;
        string strFrom = SqlHelper.String(txtFrom.Text);
        string strAuthor = SqlHelper.String(txtAuthor.Text);
        //string strTitleStyle = ddlTitleStyle.SelectedValue;
        string strTitle = txtTitle.Text.Trim();
        string strHtmlTitle = string.Empty;
        string strSelectClass = "家园活动";
        string strKeywords = txtKeywords.Text.Trim();
        string strDescription = txtDescription.Text.Trim();
        string strShortInstr = Request.Form["UEditor2"].ToString();
        strShortInstr = HttpUtility.HtmlEncode(strShortInstr);
        string strHits = txtHits.Text.Trim();
        string strOrder = txtOrder.Text.Trim();
        string strSelectId = hfArticleId.Text.Trim();
        string strAddTime = dpAddTime.Text;
        string strNotes = Request.Form["UEditor1"].ToString();
        strNotes = HttpUtility.HtmlEncode(strNotes);
        Int32.TryParse(strSelectClass, out iSelectClassId);
        Int32.TryParse(strSelectId, out iSelectId);
        Int16.TryParse(strOrder, out iOrder);
        Int16.TryParse(strHits, out iHits);
        DateTime dtAddTime = DateTime.Now;
        if (!string.IsNullOrEmpty(strAddTime))
            DateTime.TryParse(strAddTime, out dtAddTime);
        string strEndDate = SqlHelper.String(dpEndDate.Text);
        if (!string.IsNullOrEmpty(strEndDate))
            DateTime.TryParse(strEndDate, out dtEndDate);
        iStatus = Convert.ToInt16(cboxStatus.Checked);
        iIsPhotoNews = 0;
        //
        string straAllClassIds = SqlHelper.String(txtaAllClassIds.Text);
        //int intaClassId = 0;
        //Int32.TryParse(txtaClassId.Text, out intaClassId);
        //
        if (!string.IsNullOrEmpty(strSelectClass))
        {
            strHtmlTitle = strTitle;
            #region 上传图片
            string strProdImages = string.Empty;
            if (iIsPhotoNews == 1)
            {
                string strErrorMsg = string.Empty;
                strProdImages = WebBase.Comm.GetFilePath(fuProdImages, Server.MapPath("~/upload/homeevents/"), ResolveUrl("~/upload/homeevents/"), AllowUpFileExt, ref strErrorMsg);// GetFilePath(fuProdImages, iProdClass, AllowUpFileExt, ref strErrorMsg);
                //if (string.IsNullOrEmpty(strProdImages) && iSelectId == 0)
                //{
                //    FineUI.Alert.Show("图片必须上传图片！", FineUI.MessageBoxIcon.Warning);
                //    return;
                //}
            }          
            #endregion
            Model.DbClass dbClass = DalEx.DbClassEx.Select(iSelectClassId);
            string strClassIds = string.Empty;
            if (dbClass != null)
            {
                strClassIds = dbClass.CAllClassIds + iSelectClassId + "|";
            }
            else
            {
                strClassIds = "|" + iSelectClassId + "|";
            }
            Model.DbArticles dbArticles = new Model.DbArticles();
            dbArticles.AId = iSelectId;
            dbArticles.AHtmlTitle = strHtmlTitle;
            dbArticles.ATitle = strTitle;
            //dbArticles.AClassId = intaClassId;
            dbArticles.AKeywords = strKeywords;
            dbArticles.ADescription = strDescription;
            dbArticles.AShortInstr = strShortInstr;
            dbArticles.AHits = iHits;
            dbArticles.AOrder = iOrder;
            //dbArticles.AStatus = iStatus;
            dbArticles.ANote = strNotes;
            dbArticles.AAddTime = dtAddTime;
            dbArticles.AFrom = strFrom;
            dbArticles.AAuthor = strAuthor;
            dbArticles.AIsPhotoNews = iIsPhotoNews;
            dbArticles.APicture = strProdImages;
            //dbArticles.AClassIds = strClassIds;
            dbArticles.ALang = "HomeEvents";
            dbArticles.AStatus = 0;
            dbArticles.CEndDate = dtEndDate;
            dbArticles.AAllClassIds = straAllClassIds;

            string strResult = string.Empty;
            if (DalEx.DbArticlesEx.InsertEx(dbArticles, ref strResult) > 0)
            {
                sfObject.GetResetReference();
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
            }
            else
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Warning);
            }
        }
        else
        {
            FineUI.Alert.Show("请选择信息类型！", FineUI.MessageBoxIcon.Warning);
        }
    }
    protected void btnLoadData_Click(object sender, EventArgs e)
    {
        if (iSelectId > 0)//如果产品ID>0时绑定产品属性
        {
            Model.DbArticles dbArticle = DalEx.DbArticlesEx.Select(iSelectId);
            if (dbArticle != null)
            {
                PageContext.RegisterStartupScript(String.Format("updateUEditor1({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.ANote))));
                PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.AShortInstr))));
            }
        }
    }
    protected void btnGetShortInfo_Click(object sender, EventArgs e)
    {
        string content = Request.Form["UEditor1"].ToString();
        Regex regex = new Regex(@"<[^>]+>|</[^>]+>");
        content = regex.Replace(content, "");
        if (content.Length > 200)
        {
            content = content.Substring(0, 197) + "...";
        }
        PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(content)));
    }
}
